package org.colcon.web.model;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.colcon.web.model.framework.IContainer;
import org.colcon.web.model.framework.IItem;

//import com.google.gwt.i18n.shared.DateTimeFormat;

public class Item implements IItem
{
	private static final long serialVersionUID = 7553021518413774081L;
	
	public static final String
	
		// arguments
		PARENT = "parent",
		ID = "id";
	
	private String parent, id;
	private Date date;
	
	public Item()
	{
		this(new HashMap<String, String>());
	}
	
	public Item(Map<String, String> args)
	{
		this(args.get(PARENT), args.get(ID));
	}
	
	public Item(String parent, String id)
	{
		this.parent = parent;
		this.id = id;
		
		date = new Date();
	}
	
	public String getText()
	{
		//return getDateString();
		return getPath();
	}
	
	public String getId()
	{
		return id;
	}
	
	public boolean hasId()
	{
		return id != null && !id.equals(Model.NULL);
	}
	
	public void setId(String id)
	{
		this.id = id;
	}

	public String getParentPath()
	{
		return parent;
	}
	
	public void setParentPath(String parent)
	{
		this.parent = parent;
	}
	
	public String getPath()
	{
		if(getParentPath() == null)
			return getId();
		
		return getParentPath() + '/' + getId();
	}
	
	public Date getDate()
	{
		return date;
	}
	
	public void setDate(Date date)
	{
		this.date = date;
	}
	
	public String getDateString()
	{
		//TODO: format date (commented code causes error in terminal)
		
		//DateTimeFormat format = DateTimeFormat.getFormat("d MMM yyyy ', ' h:mm a");
		//return format.format(date);
		
		return date.toString();
	}

	public boolean isContainer()
	{
		return this instanceof IContainer;
	}
	
	public boolean isRoot()
	{
		return getParentPath() == null;
	}
}
